<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生" prop="teacherId">
        <el-input
          v-model="queryParams.teacherId"
          placeholder="请输入学生"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约时间" prop="processingTime">
        <el-date-picker clearable
          v-model="queryParams.processingTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预约时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="责任人" prop="teacherId">
        <el-input
          v-model="queryParams.teacherId"
          placeholder="责任人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="帮扶状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['employment:student_ssistance:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['employment:student_ssistance:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['employment:student_ssistance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="student_ssistanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="老师" align="center" prop="teacherId" />
      <el-table-column label="学生姓名" align="center" prop="realName" />
      <el-table-column label="学生微信" align="center" prop="weChat" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="预约问题处理时间" align="center" prop="processingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.processingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="问题类型" align="center" prop="questionType" />
      <el-table-column label="状态" align="center" prop="state" sortable >
        <template slot-scope="scope">
          <span style="color: green" v-if="scope.row.state==1">成功解决</span>
          <span style="color: red" v-if="scope.row.state==2">待处理</span>
          <span style="color: blue" v-if="scope.row.state==3">已移交</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-success"
            @click="completeUpdate(scope.row)"
            v-hasPermi="['employment:student_ssistance:edit']"
            v-if="scope.row.state==2"
          >完成</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-success"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['employment:student_ssistance:edit']"
            v-if="scope.row.state==2"
          >提醒</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-link"
            @click="transferUpdate(scope.row)"
            v-hasPermi="['employment:student_ssistance:edit']"
            v-if="scope.row.state==2"
            >移交</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['employment:student_ssistance:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['employment:student_ssistance:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生帮扶对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="老师" prop="teacherId">
          <el-input v-model="form.teacherId" placeholder="请输入老师" />
        </el-form-item>
        <el-form-item label="预约表Id" prop="assistanceId">
          <el-select v-model="transferForm.assistanceId" filterable>
            <el-option
              v-for="item in assistanceIds"
              :key="item.id"
              :label="item.realName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理时间" prop="processingTime">
          <el-date-picker clearable
            v-model="form.processingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预约问题处理时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="问题类型" prop="questionTypeId">
          <el-input v-model="form.questionTypeId" placeholder="请输入问题类型" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态(1、成功解决；2、待处理)" />
        </el-form-item>
        <el-form-item label="老师备注" prop="teacherNotes">
          <el-input v-model="form.teacherNotes" placeholder="请输入老师备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
<!--完成帮扶-->
    <el-dialog :title="title" :visible.sync="openComplete" width="500px" append-to-body>
      <el-form ref="form" :model="completeForm" :rules="rules" label-width="80px">
        <el-form-item label="学生姓名" prop="assistanceId">
          <el-input v-model="completeForm.realName" placeholder="" disabled />
        </el-form-item>
        <el-form-item label="帮扶状态" prop="state">
          <el-select v-model="completeForm.state" filterable>
            <el-option
              v-for="item in states"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="老师备注" prop="teacherNotes">
          <el-input type="textarea" v-model="completeForm.teacherNotes" placeholder="请输入老师备注" />
        </el-form-item>
        <el-form-item label="完成时间" prop="processingTime">
          <el-date-picker clearable
                          v-model="completeForm.processingTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择完成时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="completeSubmitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
<!--    帮扶移交-->
    <el-dialog :title="title" :visible.sync="transfer" width="500px" append-to-body>
      <el-form ref="form" :model="transferForm" :rules="rules" label-width="80px">
        <el-form-item label="移交给谁" prop="state">
          <el-select v-model="transferForm.receiveId" filterable>
            <el-option
              v-for="item in teachers"
              :key="item.teacherId"
              :label="item.teacherName"
              :value="item.teacherId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="任务备注" prop="transferNotes">
          <el-input type="textarea" v-model="transferForm.transferNotes" placeholder="请输入老师备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="transferSubmitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listStudent_ssistance, getStudent_ssistance, delStudent_ssistance, addStudent_ssistance, updateStudent_ssistance ,add_transfer, list_assistanceIds} from "@/api/employment/student_ssistance";
import {list_assistanceId} from "../../../api/employment/student_ssistance";

export default {
  name: "Student_ssistance",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生帮扶表格数据
      student_ssistanceList: [],
      assistanceIds: [],

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openComplete: false,
      transfer: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherId: null,
        assistanceId: null,
        processingTime: null,
        questionTypeId: null,
        state: null,
        teacherNotes: null,
        finishTime: null
      },
      // 表单参数
      form: {},
      completeForm:{},
      transferForm:{
        studentAssistanceId: null,
        receiveId: null,
        transferNotes:null,
      },

      states: [{
        value: '1',
        label: '成功解决'
      }, {
        value: '2',
        label: '待解决'
      },
        {
          value: '3',
          label: '未解决'
        }],
      //老师列表
      teachers:{},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学生帮扶列表 */
    getListAssistanceId() {
      this.loading = true;
      list_assistanceIds().then(response => {
        this.assistanceIds = response.rows;
        console.log(this.assistanceIds)
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询学生帮扶列表 */
    getList() {
      this.loading = true;
      listStudent_ssistance(this.queryParams).then(response => {
        this.student_ssistanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teacherId: null,
        assistanceId: null,
        createTime: null,
        processingTime: null,
        questionTypeId: null,
        state: null,
        teacherNotes: null,
        finishTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学生帮扶";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStudent_ssistance(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学生帮扶";
      });
    },
    /** 完成按钮操作 */
    completeUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStudent_ssistance(id).then(response => {
        this.completeForm = response.data;
        this.completeForm.teacherNotes = null;
        this.completeForm.processingTime = null;
        this.openComplete = true;
        this.title = "完成帮扶";
      });
    },
    /** 移交按钮操作 */
    transferUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStudent_ssistance(id).then(response => {
        this.transferForm.studentAssistanceId = id;
        this.transfer = true;
        this.title = "帮扶移交";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudent_ssistance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudent_ssistance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 帮扶成功 */
    completeSubmitForm(){
      if (this.completeForm.id != null) {
        updateStudent_ssistance(this.completeForm).then(response => {
          this.$modal.msgSuccess("完成帮扶");
          this.openComplete = false;
          this.getList();
        });
      }
    },
    /** 帮扶移交 */
    transferSubmitForm(){
        add_transfer(this.transferForm).then(response => {
          this.$modal.msgSuccess("帮扶移交");
          this.transfer = false;
          this.getList();
        });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学生帮扶编号为"' + ids + '"的数据项？').then(function() {
        return delStudent_ssistance(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('employment/student_ssistance/export', {
        ...this.queryParams
      }, `student_ssistance_${new Date().getTime()}.xlsx`)
    }
  },
  mounted() {
    this.getListAssistanceId();
  }
};
</script>
